package com.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;

import org.hibernate.Transaction;

import com.hibernate.dao.HibernateSessionFactory;
import com.hibernate.model.Basicinfo;
import com.hibernate.model.BasicinfoDAO;

public class Reg_info_check extends HttpServlet implements Filter {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doFilter(ServletRequest req, ServletResponse rsp,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
       System.out.println("check_reginfo");
       
		//if(req.getAttribute("verity")!=null)
		if(req.getAttribute("verity").equals("yes"))
			{
			    String userid=req.getParameter("userid");
			    BasicinfoDAO op=new BasicinfoDAO();
		        Transaction se;
		        Basicinfo info=null;
		   		try{
		       		se= HibernateSessionFactory.getSession().beginTransaction();
		       		info =op.findById(userid);
		       		 se.commit();
		       	 }catch( Exception e){
		       		 HibernateSessionFactory.getSession().getTransaction().rollback();
		       	 }
		       	 finally {
		       		 HibernateSessionFactory.closeSession();
		       	 }
		   		if (info!=null)
		   		   {
		   			req.setAttribute("userid_exist", "exist");
		   		    req.setAttribute("filter", "no"); 
		   		    chain.doFilter(req, rsp);
		   		    System.out.println("userid exist");
		   		   }
		   		else
		   			{req.setAttribute("filter", "pass"); 
		   		    chain.doFilter(req, rsp);}
			}
		else{
			 req.setAttribute("filter", "no"); 
			 chain.doFilter(req, rsp);
		}
		
		
		
		
		
		
		
		
	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

	/**
	 * Constructor of the object.
	 */
	public Reg_info_check() {
		super();
	}

}
